﻿using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Collections;

/// <summary>
/// Utility 的摘要说明
/// </summary>
public class Utility
{
    private static Label ms_errLabel = null; 
	public Utility()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public static void RegisterErrorLabel(Label errLabel)
    {
        ms_errLabel = errLabel;
    }

    public static void DisplayErrorLabel(string ErrMsg)
    {
        ms_errLabel.Text = ErrMsg;

    }

    public static bool HasNullValue(IOrderedDictionary dict)
    {
        bool result = false;
        for (int i = 0; i < dict.Count; ++i)
        {
            if(dict[i] == null)
            {
                result = true;
                break;
            }
        }
        return result;
    }

    public static bool IsValidScore(string str)
    {
        bool valid = true;
        int result;
        if (!int.TryParse(str, out result) || result < 0 || result > 100)
        {
            valid = false;
        }

        return valid;
    }


    public static int GetCurrentPhase()
    {
        int curPhase = 4; // 考核结果汇总
        //Get current server time
        int curDate = DateTime.Today.Day;

        ScheduleDB scheduleDB = new ScheduleDB();
        ScheduleDetail[] infos = scheduleDB.GetSchedules();
        SortedList sInfos = new SortedList();
        foreach (ScheduleDetail info in infos)
        {
            sInfos.Add(info.Phase, info);
        }

        for(int i = 0; i < sInfos.Count; ++i)
        {
            ScheduleDetail info = (ScheduleDetail) sInfos.GetByIndex(i);
            if (curDate <= info.Deadline)
            { 
                //when find the phase, get out of cycle
                curPhase = (int)info.Phase;
                break;
            }
        }

        return curPhase;
    }

    public static string PhaseNotice()
    {
        StringBuilder sb = new StringBuilder();
        int curPhase = 4; // 考核结果汇总
        //Get current server time
        int curDate = DateTime.Today.Day;

        ScheduleDB scheduleDB = new ScheduleDB();
        ScheduleDetail[] infos = scheduleDB.GetSchedules();
        SortedList sInfos = new SortedList();
        foreach (ScheduleDetail info in infos)
        {
            sInfos.Add(info.Phase, info);
        }

        for (int i = 0; i < sInfos.Count; ++i)
        {
            ScheduleDetail info = (ScheduleDetail)sInfos.GetByIndex(i);
            if (curDate < info.Deadline)
            {
                curPhase = (int) info.Phase;
                sb.Append("现在是" + info.Description + 
                    "阶段(" + info.Deadline + "号之前), 请尽快进行评分.");
				break;
            }
        }

        if(curPhase == 4)
            sb.Append("本月考核结果汇总.");

        return sb.ToString();
    }

    protected String FormatArt(string TString)
    {
        if (TString != null)
        {
            TString = TString.Replace("\r ", " <br/> ");
            TString = TString.Replace("   ", "&nbsp; ");
            return TString;
        }
        else
        {
            return TString = "无内容 ";
        }
    }

    //固定字符串后换行
    protected string SetString(string str)
    {
        StringBuilder sb = new StringBuilder(str);
        for (int i = 0; i <= sb.Length; i++)
        {
            //注意这里是使用的     "== "   如果使用   "= "   会报错的
            //这里实现的时候一半字符一换行
            if (i % (sb.Length/2) == 0)
            {
                sb.Insert(i, "\r ");
                break;
            }

        }
        return FormatArt(sb.ToString());
    }

    public static void FormatEditRow(GridView gridView, GridViewRowEventArgs e)
    {
        if (gridView == null || e == null)
            return;

        if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                int cellWidth = 0;
                foreach (object ctrl in cell.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox curText = (TextBox)ctrl;
                        int number = 0;
                        if (int.TryParse(curText.Text, out number))
                        {
                            curText.Width = Unit.Pixel(30);
                            cellWidth = 30;
                        }
                        else
                        {
                            curText.Width = Unit.Pixel(80);
                            cellWidth = 80;
                        }
                    }
                }

                if (cellWidth != 0)
                    cell.Width = Unit.Pixel(cellWidth);
            }
        }
        
    }

    public static void FormatHeader(GridView gridView, GridViewRowEventArgs e)
    {
        if (gridView == null || e == null)
            return;

        if (e.Row.RowType == DataControlRowType.Header)
        {
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                int colWidth = (e.Row.Cells[i].Text.Length + 1) / 2;
                if (colWidth > 1)
                {
                    gridView.Columns[i].ItemStyle.Width = colWidth * 16;
                }
            }
        }
    }

    public static void FormatGridview(GridView gridView,GridViewRowEventArgs e)
    {
        if (gridView == null || e == null)
            return;

        FormatHeader(gridView, e);
        FormatEditRow(gridView, e);
    }
}
